<?php

class RuanganController extends Controller
{
	public function actionIndex()
	{
            $this->renderPartial("..\admin\jsonError", array("errId" => 0, "errEx" => '"Invalid action!"'));
	}

        /*
        public function actionUpdate(
            $floorId = null, $roomType = null, $name = null, $capacity = null,
            $rx = null, $ry = null, $rw = null, $rh = null,
            $userId = null, $id = -1)
        {
            $model;
            if($id > 0) {
                $model = Ruangan::model()->findByPk($id);
                if($model === null) {
                    $this->renderPartial("..\admin\jsonError", array("errId" => 3, "errEx" => "\"Requested record doesn't exists!\""));
                    Yii::app()->end();
                }
            } else {
                $model = new Ruangan();
            }

            //save
            if($floorId)    $model->floor_id = $floorId;
            if($roomType)   $model->type_ruangan_id = $roomType;
            if($name)       $model->name = $name;
            if($capacity)   $model->capacity = $capacity;
            if($rx)         $model->rx = $rx;
            if($ry)         $model->ry = $ry;
            if($rw)         $model->rw = $rw;
            if($rh)         $model->rh = $rh;
            if($userId)     $model->user_id = $userId;
            
            try {
                if($model->save()) {
                    //Send OK JSON
                    $this->renderPartial("..\admin\jsonOK");
                } else {
                    //Send error JSON, save failed
                    $this->renderPartial("..\admin\jsonError", array("errId" => 2, "errEx" => "\"Unknown error\""));
                }
            } catch(CDbException $e) {
                $this->renderPartial("..\admin\jsonError", array("errId" => 1, "errEx" => "\"Make sure the building exists and floor number isn't taken!\""));
            }
        }
*/
        
        public function actionDelete() {
            if(Ruangan::model()->deleteByPk($_POST['id']) > 0)
                $this->renderPartial("..\admin\jsonOK");
            else
                $this->renderPartial("..\admin\jsonError", array("errId" => 0, "errEx" => "\"Unable to find requested Id\""));
        }

        public function actionAdd() {
            $model = new Ruangan();

            //Assign from client
            $model->floor_id = $_POST['floorId'];
            $model->rx = $_POST['rx'];
            $model->ry = $_POST['ry'];
            $model->rw = $_POST['rw'];
            $model->rh = $_POST['rh'];

            try {
                if($model->save()) {
                    //Send OK JSON
                    $this->renderPartial("..\admin\jsonOK", array("val" => $model->id));
                } else {
                    //Send error JSON, save failed
                    $this->renderPartial("..\admin\jsonError", array("errId" => 2, "errEx" => "\"{$model->floor_id} {$model->rx} {$model->ry} {$model->rw} {$model->rh}\""));
                }
            } catch(CDbException $e) {
                $this->renderPartial("..\admin\jsonError", array("errId" => 1, "errEx" => "\"Err2\""));
            }
        }

        public function actionUpdate() {
            $roomType   = $_POST['roomType'];
            $name       = $_POST['name'];
            $capacity   = $_POST['capacity'];
            $id         = $_POST['id'];
            $userId     = isset($_POST['userId']) ? $_POST['userId'] : null;

            //find id
            $model = Ruangan::model()->findByPk($id);
            if($model === null) {
                //Error - model not found
            } else {
                //set variables
                $model->type_ruangan_id = $roomType;
                $model->name = $name;
                $model->capacity = $capacity;
                $model->user_id = $userId;

                //Try to save
                try {
                    if($model->save()) {
                        //save succeeded, return JSON
                        $this->renderPartial("..\admin\jsonOK");
                    } else {
                        //error
                        $this->renderPartial("..\admin\jsonError", array("errId" => 0, "errEx" => "\"Unable to save\""));
                    }
                } catch(CDbException $ex) {
                    //Error on save
                        $this->renderPartial("..\admin\jsonError", array("errId" => 0, "errEx" => '"'.$ex->__toString().'"'));
                }
            }
        }

        public function actionGetInfo() {
            $model = Ruangan::model()->findByPk($_POST['id']);
            if($model === null) {
                $this->renderPartial("..\admin\jsonError", array("errId" => 0, "errEx" => "\"Requested data not found\""));
            } else {
                $this->renderPartial("info", array("model" => $model, "roomTypes" => TypeRuangan::model()->findAll()));
            }
        }

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}